//
//  UnusualOrderVC.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/12/1.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "UnusualOrderVC.h"
#import "TopBackView.h"
#import "RootViewController.h"
#import "UnusualCell.h"
#import "PassageVC.h"
#import "Common.h"
#import "WKProgressHUD.h"
#import "NullView.h"
#import <MJRefresh/MJRefresh.h>
#import "RLNetworkHelper.h"//网络判断
#import "OrderdetailVC.h"



@interface UnusualOrderVC () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic) int pageNo;
@property (nonatomic) NSInteger pageTotal;
@property (nonatomic,strong) UIView *netView;

@property (nonatomic,strong) WKProgressHUD *hud;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NullView *nullView;


@end

@implementation UnusualOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    TopBackView *topBackView= [[TopBackView alloc]initViewWithFrame:CGRectMake(0, 0, screenWidth, 64) andTitle:self.titleString];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    self.dataArray = [NSMutableArray array];
    
    topBackView.rightButton.hidden = YES;
    [topBackView.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:topBackView];
    self.pageNo = 1;

    
    self.nullView = [[NullView  alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight  - 45 * kHeight) andTitle:@"" andImage:[UIImage imageNamed:@"no_order"]];
    self.nullView.backgroundColor = GrayColor;
    
    Common *c = [[Common alloc]init];
    //创建无网络状态view
    self.netView = [c noInternet:@"网络连接失败，请检查网络设备" and:self action:@selector(NoNetPressBtn) andCGreck:CGRectMake(0, 0, Main_Width, Main_height-64)];
    
    self.view.backgroundColor = WhiteColor;
    [self initDataSources];
    [self initTableView];
    
}

-(void)initDataSources
{
    
    self.nullView.label.text = @"暂无异常回单";
    
NSString *urlString = [NSString stringWithFormat:@"%@?pageNo=%d&pageSize=10",sub_anomalywaybill,self.pageNo];

[Common requestWithUrlString:urlString contentType:application_json finished:^(id responseObj) {
    
    [self.hud dismiss: YES];
    
    if (self.pageNo == 1) {
        
        [self.dataArray removeAllObjects];
        
    }
    
    self.pageTotal = [responseObj[@"data"][@"page"][@"totalPage"] integerValue];
    
    
    if ([responseObj[@"success"] boolValue]) {
        
        [self.hud dismiss:YES];
        
        for (NSDictionary *dic in responseObj[@"data"][@"list"]) {
            [self.dataArray addObject:dic];

        }
        
        if (self.dataArray.count > 0) {
            [self.nullView removeFromSuperview];
        } else {
            [self.tableView addSubview:self.nullView];
        }
    }
    
    
    [self.tableView reloadData];
    
    
} failed:^(NSString *errorMsg) {
    [self.hud dismiss:YES];
    NSLog(@"%@",errorMsg);
}];

}

-(void)initTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.tableView];
//    self.tableView.backgroundColor = GrayColor;
    
    //mj
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    
    //无网络状态
    [self createNetWorkAndStatue];
    
}

-(void)createNetWorkAndStatue{
    
    //判断是否有网
    [self judgeNetWork];
    //创建加载状态
}

//判断是否有网
-(void)judgeNetWork{
    if (![RLNetworkHelper isConnectedToNetwork]) {
        [self.tableView addSubview:self.netView];
        self.hud = [WKProgressHUD showInView:self.view withText:@"加载中" animated:YES];
        
    }
    else{
        [self.netView removeFromSuperview];
        [self initDataSources];
    }
}
//无网络状态
-(void)NoNetPressBtn{
    if ([RLNetworkHelper isConnectedToNetwork]) {
        [self.netView removeFromSuperview];
        [self initDataSources];
    } else {
        [self.hud dismiss:YES];
    }
}

-(void)downRefresh{
    
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    self.pageNo = 1;
    
    [self judgeNetWork];
}
-(void)upRefresh{
    
    if (self.pageNo != (int)self.pageTotal) {
        self.pageNo ++;
        [self judgeNetWork];
        [self.tableView.mj_footer endRefreshing];
    }else{
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
    }
    [self.tableView.mj_header endRefreshing];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [NSString stringWithFormat:@"unusualCell"];
    UnusualCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UnusualCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
    }
    
    if (self.dataArray.count > 0) {
        
        
        NSString *string = [self backString: self.dataArray[indexPath.section][@"orderTime"]];
        NSArray *arr = [string componentsSeparatedByString:@" "];
        
        cell.timeLabel.text = arr[0];
        cell.startAddressLabel.text = [self backString: self.dataArray[indexPath.section][@"depaAddr"]];
        cell.endAddressLabel.text = [self backString: self.dataArray[indexPath.section][@"destAddr"] ];
        
//        cell.moneyLabel.text = [self backString: [self.dataArray[indexPath.row][@"orderPrice"] stringValue]];

    }
    
    
    [cell.wayButton addTarget:self action:@selector(wayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.wayButton.tag = 1100 + indexPath.section;
    
    [cell.detailButton addTarget:self action:@selector(detailButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.detailButton.tag = 1500 + indexPath.section;

    
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}


-(NSString *)backString:(NSString *)string
{
    
    if ([string isEqual:[NSNull null]]) {
        return @"";
    } else {
        return string;
    }
}

//在途信息
-(void)wayButtonAction:(UIButton *)sender
{
    PassageVC *passageV = [[PassageVC alloc]init];
    passageV.orderId = self.dataArray[sender.tag - 1100][@"id"];
    
    [self.navigationController pushViewController:passageV animated:YES];
}

//查看详情
-(void)detailButtonAction:(UIButton *)sender
{
    OrderdetailVC *orderVc = [[OrderdetailVC alloc]init];
    
    orderVc.orderID = self.dataArray[sender.tag - 1500][@"id"];
    orderVc.number = 1;
    
    [self.navigationController pushViewController:orderVc animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    OrderdetailVC *orderVc = [[OrderdetailVC alloc]init];
    orderVc.number = 1;

    orderVc.orderID = self.dataArray[indexPath.section][@"id"];
    
    [self.navigationController pushViewController:orderVc animated:YES];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90 * kHeight + 73;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]init];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, 200, cellHeight)];
    label.text = @"运单列表";
    label.font = Font(14);
    label.textColor = AddCarColor;
    
    [view addSubview:label];
    view.backgroundColor = GrayColor;
    return view;
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:YES];
    
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
